using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DTO;
using DAL;
using BUS;
using Utility;
public partial class Admin_HinhAnh_Update : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }
    private void LoadData()
    {
        try
        {
            int id = int.Parse(Request.QueryString["id"].ToString());
            txt_mahinh.Text = id.ToString();

            tbHinhAnh ha = tbHinhAnhBUS.GetByID(id);
            img_HinhMinhHoa.ImageUrl = "~/Photos/Thumb/" + ha.hinhminhhoa ;
        }
        catch
        {
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            int id = int.Parse(Request.QueryString["id"].ToString());
            tbHinhAnh ha = tbHinhAnhBUS.GetByID(id);
            
            if (fu_HinhMinhHoa.HasFile)
            {
                string filename = DateTime.Now.Ticks.ToString() + "_" + fu_HinhMinhHoa.FileName;
                fu_HinhMinhHoa.SaveAs(Server.MapPath("~/Photos/" + filename));

                System.Drawing.Image img = System.Drawing.Image.FromFile(
                    Server.MapPath("~/Photos/" + filename));

                ImageUtility.MakeThumbnailPhoto(
                    img, 100, 100, Server.MapPath("~/Photos/Thumb/"), filename);
                // xoa hinh cu
                if (ha.hinhminhhoa != "no_images.jpg")
                {
                    System.IO.File.Delete(Server.MapPath("~/Photos/" + ha.hinhminhhoa));
                    System.IO.File.Delete(Server.MapPath("~/Photos/Thumb/" + ha.hinhminhhoa));
                }
                ha.hinhminhhoa = filename;
            }
            tbHinhAnhBUS.Update(ha);
        }
        catch
        {
        }
        Response.Redirect("~/Admin/SanPham_List.aspx");
        Response.Redirect(Request.RawUrl);
    }
}
